package com.sen.app.schedul;

import java.io.File;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.sen.app.schedul.core.Env;
import com.sen.app.schedul.core.utils.DirUtils;

/**
 * 调度启动类
 * 
 * @author sen
 */
public final class Schedulstrap {
	
	private static Logger log = Logger.getLogger(Schedulstrap.class);
	
	/**
     * Daemon object used by main.
     */
    private static Schedulstrap daemon = null;
    
    /**
     * spring applicationContext
     */
    @SuppressWarnings("unused")
	private ApplicationContext applicationContext;
    
    /**
     * Initialize daemon.
     */
    public void init() {
    	initLog4jConfigurator();
    	setSchedulHome();
    	setSchedulBase();
    }
    
    /**
     * 启动
     */
    public void start() {
    	initSpringConfigurator();
    }
    
    /**
     * 停止
     */
    public void stop() {
    	
    	// FIXME
    	
    }
    
    /**
     * Destroy the Schedul Daemon.
     */
    public void destroy() {
    	
        // FIXME
    	
    }

	/**
	 * main 方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		if (daemon == null) {
			daemon = new Schedulstrap();
			try {
                daemon.init();
            } catch (Throwable t) {
                t.printStackTrace();
                return;
            }
		}
		
		String command = "start";
        if (args.length > 0) {
            command = args[args.length - 1];
        }

        if (command.equals("startd")) {
            args[args.length - 1] = "start";
            daemon.start();
        } else if (command.equals("stopd")) {
            args[args.length - 1] = "stop";
            daemon.stop();
        } else if (command.equals("start")) {
        	daemon.start();
        } else if (command.equals("stop")) {
        	daemon.stop();
        } else {
            log.warn("schedulstrap: command \"" + command + "\" does not exist.");
        }
        
	}
	
	/**
	 * 初始化log4j配置,每60秒读取一次检查一次配置文件,有更改就重新设置.
	 */
	private void initLog4jConfigurator() {
		String log4jPath = DirUtils.getConfDir() + File.separator  + "log4j.properties";
    	PropertyConfigurator.configureAndWatch(log4jPath);
	}
	
	/**
	 * 初始化spring配置
	 */
	private void initSpringConfigurator() {
		String applicationContextXml = DirUtils.getConfDir() + File.separator  
		    + "applicationContext.xml";
    	File xml = new File(applicationContextXml);
    	if (null != xml && !xml.exists()) {
    		log.warn("未找到applicationContext.xml配置文件,程序退出!");
    		return;
    	}
    	String[] configXmls = new String[] { xml.getAbsolutePath() };
    	applicationContext = new FileSystemXmlApplicationContext(configXmls);
	}
	
	/**
     * Set the <code>catalina.base</code> System property to the current
     * working directory if it has not been set.
     */
    private void setSchedulBase() {

        if (System.getProperty(Env.SCHEDUL_BASE_KEY) != null)
            return;
        if (System.getProperty(Env.SCHEDUL_HOME_KEY) != null)
            System.setProperty(Env.SCHEDUL_BASE_KEY,
                               System.getProperty(Env.SCHEDUL_HOME_KEY));
        else
            System.setProperty(Env.SCHEDUL_BASE_KEY,
                               System.getProperty("user.dir"));

    }

	/**
     * Set the <code>app.home</code> System property to the current
     * working directory if it has not been set.
     */
    private void setSchedulHome() {

        if (System.getProperty(Env.SCHEDUL_HOME_KEY) != null)
            return;
        
        System.setProperty(Env.SCHEDUL_HOME_KEY,
                           System.getProperty("user.dir"));

    }
    
}
